using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Windows.Forms;

using VSTrid = Microsoft.VisualStudio.VirtualTreeGrid;

using DslEditorPowerToy.Controls.VirtualTreeGrid;
using DslEditorPowerToy.Controls.VirtualTreeGrid.Branches;

using DslEditorPowerToy.Controls.Editors.Special.DomainModelTreeGrid.Data;

namespace DslEditorPowerToy.Controls.Editors.Special.DomainModelTreeGrid.Branches
{
    internal abstract class DomainElementBranch : DefaultNavigationBranchBase
    {
        private IDomainElement _branchData = null;

        #region Constructors
        protected DomainElementBranch(VirtualTreeGridControl treeControl,
            IDomainElement branchData, Type branchDataItemType)
            : base(treeControl, branchDataItemType)
        {
            this._branchData = branchData;
        }
        #endregion

        #region IBranch Members
        /// <summary>
        /// Return the features supported by this navigationBranch.
        /// </summary>
        /// <value></value>
        public override VSTrid.BranchFeatures Features
        {
            get
            {
                VSTrid.BranchFeatures features = base.Features |
                    VSTrid.BranchFeatures.Expansions |
                    VSTrid.BranchFeatures.ImmediateMouseLabelEdits |
                    VSTrid.BranchFeatures.ExplicitLabelEdits |
                    VSTrid.BranchFeatures.Realigns |
                    VSTrid.BranchFeatures.InsertsAndDeletes |
                    VSTrid.BranchFeatures.JaggedColumns |
                    VSTrid.BranchFeatures.PositionTracking;

                return features;
            }
        }
        /// <summary>
        /// Return the count of visible items in this navigationBranch.
        /// </summary>
        /// <value></value>
        public override int VisibleItemCount
        {
            get
            {
                IList rows = this.GetElementRows();
                if (null == rows)
                    return 0;
                else
                    return rows.Count;
            }
        }
        #endregion

        #region IDataItemBranch Members
        /// <summary>
        /// Returns the item at specified row
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public override object GetDataItem(int row)
        {
            return this.GetElementAtRow(row);
        }
        /// <summary>
        /// Return the validation error for the specified property and value
        /// </summary>
        /// <param name="newValue"></param>
        /// <returns></returns>
        public override string GetDataItemValidationMessage(string propertyName, string newValue)
        {
            //Update dataItem
            return this.BranchData.GetValidationMessage(propertyName, newValue);
        }
        #endregion

        #region BranchBase Members
        protected override ImageList DataItemImageList
        {
            get
            {
                return BranchItemGlyphs.ImageList;
            }
        }
        protected override short GetDataItemImageIndex(int row, int column)
        {
            if (0 == column)
                return BranchItemGlyphs.DomainRelationship;
            else
                return base.GetDataItemImageIndex(row, column);
        }
        #endregion

        #region DefaultNavigationBranchBase Members
        /// <summary>
        /// Gets whether specified row is a new row
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public override bool IsRowNewPlaceholderRow(int row)
        {
            return false;
        }
        #endregion

        #region Protected Properties
        protected IDomainElement BranchData
        {
            get
            {
                return this._branchData;
            }
        }
        #endregion

        #region Protected Methods
        protected virtual IDomainElement GetElementAtRow(int row)
        {
            DomainElementCollection<IDomainElement> elements = GetElementRows();
            return ((row < elements.Count) ? elements[row] : null);
        }
        protected abstract DomainElementCollection<IDomainElement> GetElementRows();
        #endregion
    }
}
